﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.Business;

namespace University.Workshop.WinApp
{
    public partial class CheckInForm :TemplateForm
    {
        DateTime firstDate;
        int idType = 0;
        Boolean isFromPrincipal = false;
        public CheckInForm()
        {
            InitializeComponent();
            try
            {
                SearchClientSimple search = new SearchClientSimple("Check In");
                search.ShowDialog();
                if (!string.IsNullOrEmpty(Program.ciClient))
                {
                    ciLabel.Text = Program.ciClient;
                    nombreLabel.Text = ClientManager.GetFullNameFromCI(Program.ciClient);
                    Program.ciClient = null;
                    firstDate = DateTime.Today;
                    RoomStuffManager.GetAllBookedAndTokenRoomsForADay(DateTime.Today, roomsDataGridView, 0);
                    this.StyleTheCells();
                    this.comboBoxTypes.SelectedIndex = 4;
                }
                else
                {
                    throw new ArgumentException("Error En la Busqueda del Cliente");

                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                this.Close();
            }
            //this.checkOutDateTimePicker.Value=new DateTime(2011,05,09);
        }
        public CheckInForm(string roomRow, DateTime checkOut, Boolean isPrincipal)
        {
            InitializeComponent();
            try
            {
                SearchClientSimple search = new SearchClientSimple("Check In");
                search.ShowDialog();
                if (!string.IsNullOrEmpty(Program.ciClient))
                {
                    ciLabel.Text = Program.ciClient;
                    nombreLabel.Text = ClientManager.GetFullNameFromCI(Program.ciClient);
                    Program.ciClient = null;
                    firstDate = DateTime.Today;
                    RoomStuffManager.GetAllBookedAndTokenRoomsForADay(DateTime.Today, roomsDataGridView, 0);
                    this.StyleTheCells();
                    this.comboBoxTypes.SelectedIndex = 4;
                    this.checkOutDateTimePicker.Value = checkOut;
                    roomLabel.Text = roomRow;
                    this.tipoLabel.Visible = false;
                    this.comboBoxTypes.Visible = false;
                    this.roomsDataGridView.Visible = false;
                    this.isFromPrincipal = true;
                    this.ControlBox = true;
                }
                else
                {
                    throw new ArgumentException("Error En la Busqueda del Cliente");

                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                this.Close();
            }
            
        }
        public void StyleTheCells()
        {
            int tamRows = roomsDataGridView.Rows.Count;
            string header = firstDate.ToShortDateString().Substring(0, 5);
                for (int rowIterator = 0; rowIterator < tamRows; rowIterator++)
                {
                    if (roomsDataGridView[header, rowIterator].Value.ToString() == "Taken")
                    {
                        roomsDataGridView[header, rowIterator].Style.BackColor = Color.Red;
                    }
                    if (roomsDataGridView[header, rowIterator].Value.ToString() == "Booked")
                    {
                        roomsDataGridView[header, rowIterator].Style.BackColor = Color.Salmon;
                    }
                    if (roomsDataGridView[header, rowIterator].Value.ToString() == "Free")
                    {
                        roomsDataGridView[header, rowIterator].Style.BackColor = Color.Aquamarine;
                    }
                }
            }

        private void comboBoxTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            String roomType = (String)comboBoxTypes.Text.Trim();
            idType = RoomStuffManager.GetByName(roomType);
            roomsDataGridView.Rows.Clear();
            roomsDataGridView.Columns.Clear();
            RoomStuffManager.GetAllBookedAndTokenRoomsForADay(firstDate, roomsDataGridView, idType);
            this.StyleTheCells();
        }


        private void registrarButton_Click(object sender, EventArgs e)
        {
            try
            {
                CheckInManager.CheckInClient(roomLabel.Text, ciLabel.Text, this.checkInDateTimePicker.Value, checkOutDateTimePicker.Value);
                MessageBox.Show("CheckIn Exitoso!");
                if (this.isFromPrincipal) { this.Close(); }
                this.roomsDataGridView.Rows.Clear();
                this.roomsDataGridView.Columns.Clear();
                RoomStuffManager.GetAllBookedAndTokenRoomsForADay(DateTime.Today, roomsDataGridView, 0);
                this.StyleTheCells();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void roomsDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            roomLabel.Text = this.roomsDataGridView.Rows[e.RowIndex].Cells[0].Value.ToString();
        }

        private void CheckInForm_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }        
    }
}
